<?php
$var = 0;
include '../model/connexion.php';
include '../model/model_sql.php';
if (isset($_POST['connexion'])) {
    if (isset($_POST['login']) && isset($_POST['mdp'])) {
        $login = htmlentities($_POST['login']);
        $mdp = htmlentities($_POST['mdp']);
        $mdp = addslashes($mdp);
        $login = addslashes($login);


        $myconn = new connexion($login, $mdp);
        if ($myconn->connexion() == -1) {
            $var = 1;
            $message = "Erreur, entrez vos identifiants à nouveau.";
        }
    }
}
?>

<link href='http://fonts.googleapis.com/css?family=Permanent+Marker' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Quicksand:700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Gloria+Hallelujah' rel='stylesheet' type='text/css'>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.0/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.0/js/bootstrap-toggle.min.js"></script>

<style type="text/css">

    .navbarmid{
        -webkit-box-shadow: 0px 0px 20px 0px rgba(0,0,0,0.75);
        -moz-box-shadow: 0px 0px 20px 0px rgba(0,0,0,0.75);
        box-shadow: 0px 0px 20px 0px rgba(0,0,0,0.75);
    }
	
    #mynavbar{
        margin-bottom:100px;
    }
	
	.navbar-form{
		padding:0;
		margin-right:10%;
	}

    #mynavbar.stick {
        position:fixed;
        top:0;
        z-index:100;
        width:100%;
    }

    #megaheader{
        max-width:100%;
        height:400px;
        background-image:url(../img/tropical.jpg);
        background-repeat:repeat;
		overflow:hidden;
    }
	
	#megatitle{
		font-family: 'Gloria Hallelujah', cursive;
		font-size:200px;
		margin-top:70px;
		text-align:center;
		color:white;
	}	
	
	.toggle{
		margin: 8px 0 8px 10%;
		float:left;
	}
	
	.container-fluid{
		padding:0;
	}
	
	.container-fluid .navbar-collapse{
		padding:0;
	}
	
</style>

<script>

    var sec = false;
	
	function sticky_relocate(){
        var window_top = $(window).scrollTop();
        var div_top = $('#sticky-anchor').offset().top;
        if (window_top > div_top) {
            $('#mynavbar').addClass('stick');
			$('#bestfluid').css('height','115px');
        } else {
            $('#mynavbar').removeClass('stick');
			$('#bestfluid').css('height','0');
        }
    }

    $(function(){
        $(window).scroll(sticky_relocate);
        sticky_relocate();
    });
	
	$(function(){
		$('#toggle-event').change(function() {
			var all = document.getElementsByClassName('content');
			var mine = document.getElementsByClassName('content ' + <?php echo $_SESSION['id'];?>);
			if($(this).prop('checked')){
				for(var i = 0; i < all.length; i++){
						all[i].style.display="none";
				}
				for(var i = 0; i < mine.length; i++){
						mine[i].style.display="block";
				}
				sec = true;
			}
			else{
				for(var i = 0; i < all.length; i++){
					all[i].style.display="block";
				}
				sec = false;
			}
		})	
	})
	
	 function cat_change(){
	 
		var plus = "";
		if(sec){plus = <?php echo $_SESSION['id'];?>}
		var all = document.getElementsByClassName('content ' + plus);
		var e = document.getElementById('sel_cat');
		var sel = e.options[e.selectedIndex].value;
		
		var keep = document.getElementsByClassName(plus +' cat' + sel);
		
		if(sel!=-1){	
			for(var i = 0; i < all.length; i++){
				all[i].style.display="none";
			}
			for(var i = 0; i < keep.length; i++){
				keep[i].style.display="block";
			}
		}else{
			for(var i = 0; i < all.length; i++){
				all[i].style.display="block";
			}
		}
			
	 }

</script>

<div id="megaheader">
	<h1 id="megatitle">BLOWG</h1>
</div>
<div id="sticky-anchor"></div>
<div id="mynavbar">
    <nav class="navbarmid" style="width:100%;z-index:2;background:#FFFFFF;">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header"></div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
			
                <?php if (!isset($_SESSION['login'])) { ?>
                    <form class="navbar-form navbar-right" method="POST" action="">
					<a id="testo" href="index.php" class="btn btn-default" style="left:0;"><span class="glyphicon glyphicon-home"></span></a>
                        <?php
                        if ($var == 1) {
                            echo "<h3 style=\"float:left;margin:3px 10px\">" . $message . "</h3>";
                        }
                        ?>
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Login" name="login">
                            <input type="password" class="form-control" placeholder="Mot de passe" name="mdp">
                        </div>
                        <input type="submit" value="Connexion" name="connexion" class="btn btn-primary btn-success"/>
                        <a href="../vue/FormulaireInscription.php" class="btn btn-primary btn-primary">Inscription</a>
                    </form>
                <?php } else { ?>
					<?php if($_SERVER['PHP_SELF']=='/blog/vue/index.php'){
								$checkbox = false;
								if($_SESSION['id_droit']==2){
									echo "<input id=\"toggle-event\" type=\"checkbox\" data-toggle=\"toggle\" data-on=\"Mes articles\" data-off=\"Tous\">";
									$checkbox = true;
								}
								if($checkbox){echo "<select onchange=\"cat_change()\" style=\"width:200px;margin:8px 0 8px 8px;\" name=\"sel_cat\" class=\"btn btn-default\" id=\"sel_cat\">";}
								else{echo "<select onchange=\"cat_change()\" style=\"width:200px;margin:8px 0 8px 10%;\" name=\"sel_cat\" class=\"btn btn-default\" id=\"sel_cat\">";}
						echo "<option value=\"-1\">Toutes les catégories</option>";
						$myvar = new SQLBlog();
                        if ($var = $myvar->AfficherCategories()) {
                            foreach ($var as $line) {
                                echo "<option value=\"" . $line['id'] . "\">" . $line['nom'] . "</option>";
                            }
                        }
						echo "</select>";
					}?>
                    <form class="navbar-form navbar-right" method="POST" action="../controleur/deconnexion.php">
						<a href="index.php" class="btn btn-default" style="left:0;"><span id="testo" class="glyphicon glyphicon-home"></span></a>
                        <h2 style="float:left;margin:0 15px;font-size:35px;"><?php echo $_SESSION['prenom'].' '.$_SESSION['nom'];?></h2>
						<?php if(isset($_SESSION['login'])) echo "<a id=\"testwrench\" href =\"gestionUtilisateur.php\" class=\"btn btn-default\"><span class=\"glyphicon glyphicon-wrench\"></span></a>";?>
						<?php if(isset($_SESSION['login']) && isset($_SESSION['id_droit']) && $_SESSION['id_droit'] == 1) echo "<a href=\"gestionDroit.php\" class=\"btn btn-default\"><span class=\"glyphicon glyphicon-user\"></span>&nbsp;<span class=\"glyphicon glyphicon-list\"></span></a>&nbsp;<a href=\"../vue/ajouter.php\" class=\"btn btn-primary btn-primary\">Creer un article</a>";?>
						<?php if(isset($_SESSION['login']) && isset($_SESSION['id_droit']) && $_SESSION['id_droit'] == 2) echo "<a href=\"../vue/ajouter.php\" class=\"btn btn-primary btn-primary\">Creer un article</a>";?>
						<input type="submit" value="Deconnexion" name="deconnexion" class="btn btn-primary btn-danger"/>
                    </form>
                <?php } ?>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>
</div>
<div id="bestfluid"></div>